﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KKU.RBSFPP.Shared.DAC;
using KKU.RBSFPP.Shared.Engines;

namespace KKU.RBSFPP.Web.Administrator.Controllers
{
    [Authorize(Users = "admin")]    
    public class ProductController : Controller
    {
        private RBSFPPModelContainer db = new RBSFPPModelContainer();

        //
        // GET: /Product/

        public ActionResult Index()
        {
            var products = db.Products.Include("Category").Include("ProductUnit").Include("ProductPricingPerUnit");
            return View(products.ToList().Display());
        }

        //
        // GET: /Product/Details/5

        public ActionResult Details(int id = 0)
        {
            Product product = db.Products.Single(p => p.Id == id);
            if (product == null)
            {
                return HttpNotFound();
            }
            return View(product);
        }

        //
        // GET: /Product/Create

        public ActionResult Create()
        {
            ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name");
            ViewBag.ProductUnitId = new SelectList(db.ProductUnits, "Id", "Name");
            ViewBag.ProductPricingPerUnitId = new SelectList(db.ProductPricingPerUnits, "Id", "Name");
            return View();
        }

        //
        // POST: /Product/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Product product,string buyInput,string sellInput)
        {
            double buy;          
            if (double.TryParse(buyInput, out buy) == false)
            {
                ModelState.AddModelError("buyInput", "ราคาซื้อจะต้องเป็นตัวเลขเท่านั้น");
            }
            else if (buy <= 0)
            {
                ModelState.AddModelError("buyInput", "ราคาซื้อจะต้องมากกว่า 0 ขึ้นไปเท่านั้น");
            }
           

            double sell;
            if (double.TryParse(sellInput, out sell) == false)
            {
                ModelState.AddModelError("sellInput", "ราคาขายจะต้องเป็นตัวเลขเท่านั้น");
            }
            else if (buy <= 0)
            {
                ModelState.AddModelError("sellInput", "ราคาขายจะต้องมากกว่า 0 ขึ้นไปเท่านั้น");
            }


            if (ModelState.IsValid)
            {
                var now = DateTime.Now;
                var pPrice = new ProductPricingPerUnit
                {
                    CreateOn = now,
                    PurchasingPrice = buy,
                    SellingPrice = sell,
                    Name = "Unknow"
                };
                db.ProductPricingPerUnits.AddObject(pPrice);

                var pPricTracking = new ProductPricingPerUnitTracking
                {
                    CreateOn = now,
                    PurchasingPrice = buy,
                    SellingPrice = sell,
                    Name = "Unknow"
                };
                db.ProductPricingPerUnitTrackings.AddObject(pPricTracking);

                product.Create();
                product.ProductPricingPerUnit = pPrice;
                product.ProductPricingPerUnitTrackings.Add(pPricTracking);
                db.Products.AddObject(product);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name", product.CategoryId);
            ViewBag.ProductUnitId = new SelectList(db.ProductUnits, "Id", "Name", product.ProductUnitId);
            ViewBag.ProductPricingPerUnitId = new SelectList(db.ProductPricingPerUnits, "Id", "Name", product.ProductPricingPerUnitId);
            return View(product);
        }

        //
        // GET: /Product/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Product product = db.Products.Single(p => p.Id == id);
            if (product == null)
            {
                return HttpNotFound();
            }
            ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name", product.CategoryId);
            ViewBag.ProductUnitId = new SelectList(db.ProductUnits, "Id", "Name", product.ProductUnitId);
            ViewBag.ProductPricingPerUnitId = new SelectList(db.ProductPricingPerUnits, "Id", "Name", product.ProductPricingPerUnitId);
            ViewBag.buy = product.ProductPricingPerUnit.PurchasingPrice;
            ViewBag.sell = product.ProductPricingPerUnit.SellingPrice;
            return View(product);
        }

        //
        // POST: /Product/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Product product, double buy, double sell)
        {
            if (ModelState.IsValid)
            {
                var now = DateTime.Now;
                var pPrice = new ProductPricingPerUnit
                {
                    CreateOn = now,
                    PurchasingPrice = buy,
                    SellingPrice = sell,
                    Name = "Unknow"
                };
                db.ProductPricingPerUnits.AddObject(pPrice);

                var pPricTracking = new ProductPricingPerUnitTracking
                {
                    CreateOn = now,
                    PurchasingPrice = buy,
                    SellingPrice = sell,
                    Name = "Unknow"
                };
                db.ProductPricingPerUnitTrackings.AddObject(pPricTracking);

                db.Products.Attach(product);
                product.ProductPricingPerUnit = pPrice;
                product.ProductPricingPerUnitTrackings.Add(pPricTracking);
                db.ObjectStateManager.ChangeObjectState(product, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name", product.CategoryId);
            ViewBag.ProductUnitId = new SelectList(db.ProductUnits, "Id", "Name", product.ProductUnitId);
            ViewBag.ProductPricingPerUnitId = new SelectList(db.ProductPricingPerUnits, "Id", "Name", product.ProductPricingPerUnitId);
            return View(product);
        }

        //
        // GET: /Product/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Product product = db.Products.Single(p => p.Id == id);
            if (product == null)
            {
                return HttpNotFound();
            }
            return View(product);
        }

        //
        // POST: /Product/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Product product = db.Products.Single(p => p.Id == id);
            //db.Products.DeleteObject(product);
            product.Delete();
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}